//
//  JSShareViewController.m
//  JigSaw
//
//  Created by Slim Wang on 15/2/13.
//  Copyright (c) 2015年 LITB. All rights reserved.
//

#import "JSShareViewController.h"
#import "UIImage+Help.h"
#import <ShareSDK/ShareSDK.h>
#import "UIColor+Help.h"
@interface JSShareViewController ()

@property(nonatomic, weak) IBOutlet UIImageView* iv;
@property(nonatomic, weak) IBOutlet UIView*      blurView;

@property(nonatomic, weak) IBOutlet UIButton* homeBtn;
@property(nonatomic, weak) IBOutlet UIButton* backBtn;

@property(nonatomic, weak) IBOutlet UIButton* btn0;
@property(nonatomic, weak) IBOutlet UIButton* btn1;
@property(nonatomic, weak) IBOutlet UIButton* btn2;
@property(nonatomic, weak) IBOutlet UIButton* btn3;
@property(nonatomic, weak) IBOutlet UIButton* btn4;
@property(nonatomic, weak) IBOutlet UIButton* btn5;
@property(nonatomic, weak) IBOutlet UIButton* btn6;
@property(nonatomic, weak) IBOutlet UILabel*  label0;
@property(nonatomic, weak) IBOutlet UILabel*  label1;
@property(nonatomic, weak) IBOutlet UILabel*  label2;
@property(nonatomic, weak) IBOutlet UILabel*  label3;
@property(nonatomic, weak) IBOutlet UILabel*  label4;
@property(nonatomic, weak) IBOutlet UILabel*  label5;
@property(nonatomic, weak) IBOutlet UILabel*  label6;

@end

@implementation JSShareViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.blurView.backgroundColor = [UIColor colorWithHex:0x000000 alpha:0.6];
    @weakify(self)
    self.backBtn.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self)
        [self.iv removeFromSuperview];
        [self onBackButtonPressed:self.backBtn];
        return [RACSignal empty];
    }];
    self.homeBtn.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self)
        [self.navigationController popToRootViewControllerAnimated:YES];
        return [RACSignal empty];
    }];
    if (0) {
        self.btn0.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            @strongify(self)
            [self shareWithType:ShareTypeWeixiSession];
            return [RACSignal empty];
        }];
        self.label0.text = NSLocalizedString(@"微信好友", @"");

        self.btn1.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            @strongify(self)
            [self shareWithType:ShareTypeWeixiTimeline];
            return [RACSignal empty];
        }];
        self.label1.text = NSLocalizedString(@"朋友圈", @"");

        self.btn2.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            @strongify(self)
            [self shareWithType:ShareTypeQQ];
            return [RACSignal empty];
        }];
        self.label2.text = NSLocalizedString(@"QQ好友", @"");

        self.btn3.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            @strongify(self)
            [self shareWithType:ShareTypeQQSpace];
            return [RACSignal empty];
        }];
        self.label3.text = NSLocalizedString(@"QQ空间", @"");

        self.btn4.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            @strongify(self)
            [self shareWithType:ShareTypeSinaWeibo];
            return [RACSignal empty];
        }];
        self.label4.text = NSLocalizedString(@"新浪微博", @"");

        self.btn5.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            @strongify(self)
            [self shareWithType:ShareTypeFacebook];
            return [RACSignal empty];
        }];
        self.label5.text = NSLocalizedString(@"Fackbook", @"");

        self.btn6.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            @strongify(self)
            [self shareWithType:ShareTypeTwitter];
            return [RACSignal empty];
        }];
        self.label6.text = NSLocalizedString(@"Twitter", @"");
    } else {
        [self.btn0 setImage:[UIImage imageNamed:@"share_fb"] forState:UIControlStateNormal];
        [self.btn0 setImage:[UIImage imageNamed:@"share_fb_click"] forState:UIControlStateHighlighted];
        self.btn0.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            @strongify(self)
            [self shareWithType:ShareTypeFacebook];
            return [RACSignal empty];
        }];
        self.label0.text = NSLocalizedString(@"Fackbook", @"");

        [self.btn1 setImage:[UIImage imageNamed:@"share_twitter"] forState:UIControlStateNormal];
        [self.btn1 setImage:[UIImage imageNamed:@"share_twitter_click"] forState:UIControlStateHighlighted];
        self.btn1.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            @strongify(self)
            [self shareWithType:ShareTypeTwitter];
            return [RACSignal empty];
        }];
        self.label1.text = NSLocalizedString(@"Twitter", @"");

        [self.btn2 setImage:[UIImage imageNamed:@"share_wx"] forState:UIControlStateNormal];
        [self.btn2 setImage:[UIImage imageNamed:@"share_wx_click"] forState:UIControlStateHighlighted];
        self.btn2.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            @strongify(self)
            [self shareWithType:ShareTypeWeixiSession];
            return [RACSignal empty];
        }];
        self.label2.text = NSLocalizedString(@"微信好友", @"");

        [self.btn3 setImage:[UIImage imageNamed:@"share_pyq"] forState:UIControlStateNormal];
        [self.btn3 setImage:[UIImage imageNamed:@"share_pyq_click"] forState:UIControlStateHighlighted];
        self.btn3.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            @strongify(self)
            [self shareWithType:ShareTypeWeixiTimeline];
            return [RACSignal empty];
        }];
        self.label3.text = NSLocalizedString(@"朋友圈", @"");

        [self.btn4 setImage:[UIImage imageNamed:@"share_qq"] forState:UIControlStateNormal];
        [self.btn4 setImage:[UIImage imageNamed:@"share_qq_click"] forState:UIControlStateHighlighted];
        self.btn4.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            @strongify(self)
            [self shareWithType:ShareTypeQQ];
            return [RACSignal empty];
        }];
        self.label4.text = NSLocalizedString(@"QQ好友", @"");

        [self.btn5 setImage:[UIImage imageNamed:@"share_qzone"] forState:UIControlStateNormal];
        [self.btn5 setImage:[UIImage imageNamed:@"share_qzone_click"] forState:UIControlStateHighlighted];
        self.btn5.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            @strongify(self)
            [self shareWithType:ShareTypeQQSpace];
            return [RACSignal empty];
        }];
        self.label5.text = NSLocalizedString(@"QQ空间", @"");

        [self.btn6 setImage:[UIImage imageNamed:@"share_weibo"] forState:UIControlStateNormal];
        [self.btn6 setImage:[UIImage imageNamed:@"share_weibo_click"] forState:UIControlStateHighlighted];
        self.btn6.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            @strongify(self)
            [self shareWithType:ShareTypeSinaWeibo];
            return [RACSignal empty];
        }];
        self.label6.text = NSLocalizedString(@"新浪微博", @"");

    }

}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.iv.image = self.bgImage;
}

- (void)onBackButtonPressed:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:NULL];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

- (void)shareWithType:(ShareType)type
{
    id<ISSContent> publishContent = [ShareSDK content:@""
                                       defaultContent:@""
                                                image:[ShareSDK pngImageWithImage:self.image]
                                                title:@"Camera360"
                                                  url:@"http://www.mob.com"
                                          description:@""
                                            mediaType:SSPublishContentMediaTypeImage];
    [ShareSDK clientShareContent:publishContent type:type statusBarTips:NO result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
        if (SSResponseStateFail == state && ShareTypeSinaWeibo != type && ShareTypeFacebook !=type && ShareTypeTwitter != type) {
            NSString* errorMsg = @"";
            switch (type) {
                case ShareTypeWeixiSession:
                case ShareTypeWeixiTimeline:
                    errorMsg = NSLocalizedString(@"未安装微信客户端", @"");
                    break;
                case ShareTypeQQ:
                    errorMsg = NSLocalizedString(@"未安装QQ客户端", @"");
                    break;
                case ShareTypeQQSpace:
                    errorMsg = NSLocalizedString(@"未安装QQ空间客户端", @"");
                    break;
                default:
                    break;
            }
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"" message:errorMsg delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"确定", @""), nil];
            [alert show];
        }
    }];
}

//- (void)shareWeixiSession
//{
//    id<ISSContent> publishContent = [ShareSDK content:@""
//                                       defaultContent:@""
//                                                image:[ShareSDK pngImageWithImage:self.image]
//                                                title:@"JigSaw"
//                                                  url:@"http://www.mob.com"
//                                                description:@""
//                                                mediaType:SSPublishContentMediaTypeImage];
//    //        [publishContent addWeixinSessionUnitWithType:INHERIT_VALUE
//    //                                             content:INHERIT_VALUE
//    //                                               title:NSLocalizedString(@"TEXT_HELLO_WECHAT_SESSION", @"Hello 微信好友!")
//    //                                                 url:INHERIT_VALUE
//    //                                               image:INHERIT_VALUE
//    //                                        musicFileUrl:nil
//    //                                             extInfo:nil
//    //                                            fileData:nil
//    //                                        emoticonData:nil];
//    [ShareSDK clientShareContent:publishContent type:ShareTypeWeixiSession statusBarTips:NO result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
//        
//    }];
//}
//- (void)shareWeixiTimeline
//{
//    id<ISSContent> publishContent = [ShareSDK content:@""
//                                       defaultContent:@""
//                                                image:[ShareSDK pngImageWithImage:self.image]
//                                                title:@"JigSaw"
//                                                  url:@"http://www.mob.com"
//                                          description:@""
//                                            mediaType:SSPublishContentMediaTypeImage];
////    [publishContent addWeixinTimelineUnitWithType:[NSNumber numberWithInteger:SSPublishContentMediaTypeImage]
////                                          content:INHERIT_VALUE
////                                            title:NSLocalizedString(@"TEXT_HELLO_WECHAT_TIMELINE", @"Hello 微信朋友圈!")
////                                              url:@""
////                                            image:INHERIT_VALUE
////                                     musicFileUrl:@""
////                                          extInfo:nil
////                                         fileData:nil
////                                     emoticonData:nil];
//
//    [ShareSDK clientShareContent:publishContent type:ShareTypeWeixiTimeline statusBarTips:NO result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
//        
//    }];
//}
//
//- (void)shareQQ
//{
//    id<ISSContent> publishContent = [ShareSDK content:@""
//                                       defaultContent:@""
//                                                image:[ShareSDK pngImageWithImage:self.image]
//                                                title:@"JigSaw"
//                                                  url:@"http://www.mob.com"
//                                          description:@""
//                                            mediaType:SSPublishContentMediaTypeImage];
//    //    [publishContent addQQUnitWithType:INHERIT_VALUE
//    //                              content:INHERIT_VALUE
//    //                                title:@"Hello QQ!"
//    //                                  url:INHERIT_VALUE
//    //                                image:INHERIT_VALUE];
//    [ShareSDK clientShareContent:publishContent type:ShareTypeQQ statusBarTips:NO result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
//        
//    }];
//}
//
//- (void)shareQZone
//{
//    id<ISSContent> publishContent = [ShareSDK content:@""
//                                       defaultContent:@""
//                                                image:[ShareSDK pngImageWithImage:self.image]
//                                                title:@"JigSaw"
//                                                  url:@"http://www.mob.com"
//                                          description:@""
//                                            mediaType:SSPublishContentMediaTypeImage];
//    [ShareSDK clientShareContent:publishContent type:ShareTypeQQSpace statusBarTips:NO result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
//        
//    }];
//}
//
//- (void)shareSinaWeibo
//{
//    id<ISSContent> publishContent = [ShareSDK content:@""
//                                       defaultContent:@""
//                                                image:[ShareSDK pngImageWithImage:self.image]
//                                                title:@"JigSaw"
//                                                  url:@"http://www.mob.com"
//                                          description:@""
//                                            mediaType:SSPublishContentMediaTypeImage];
//    [ShareSDK clientShareContent:publishContent type:ShareTypeSinaWeibo statusBarTips:NO result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
//        
//    }];
//
//}
//
//
//- (void)shareFaceBook
//{
//    id<ISSContent> publishContent = [ShareSDK content:@""
//                                       defaultContent:@""
//                                                image:[ShareSDK pngImageWithImage:self.image]
//                                                title:@"JigSaw"
//                                                  url:@"http://www.mob.com"
//                                          description:@""
//                                            mediaType:SSPublishContentMediaTypeImage];
//    [ShareSDK clientShareContent:publishContent type:ShareTypeFacebook statusBarTips:NO result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
//        
//    }];
//}
//
//- (void)shareTwitter
//{
//    id<ISSContent> publishContent = [ShareSDK content:@""
//                                       defaultContent:@""
//                                                image:[ShareSDK pngImageWithImage:self.image]
//                                                title:@"JigSaw"
//                                                  url:@"http://www.mob.com"
//                                          description:@""
//                                            mediaType:SSPublishContentMediaTypeImage];
//    [ShareSDK clientShareContent:publishContent type:ShareTypeTwitter statusBarTips:NO result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
//        
//    }];
//}


//- (void)addRightButton
//{
//    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithTitle:@"保存到相册"
//                                                                       style:UIBarButtonItemStylePlain
//                                                                      target:self
//                                                                      action:@selector(onRightButtonPressed:)];
//    rightBarButton.tintColor = [UIColor whiteColor];
//    self.navigationItem.rightBarButtonItem = rightBarButton;
//}
//
//- (void)onRightButtonPressed:(id)sender
//{
//    UIImageWriteToSavedPhotosAlbum(self.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
//}

//- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
//{
//    UIAlertView *alert;
//    if (error == nil)
//    {
//        alert = [[UIAlertView alloc] initWithTitle:nil message:@"保存成功!" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil];
//        [alert show];
//    }
//    else
//    {
//        alert = [[UIAlertView alloc] initWithTitle:nil message:@"保存失败，请重试" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil];
//        [alert show];
//    }
//}
@end
